{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# COCO Image Viewer\n",
    "This notebook will allow you to view details about a COCO dataset and preview segmentations on annotated images.\n",
    "Learn more about it at: http://cocodataset.org/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.441736Z",
     "start_time": "2019-07-27T08:58:05.075740Z"
    }
   },
   "outputs": [],
   "source": [
    "import IPython\n",
    "import os\n",
    "import json\n",
    "import random\n",
    "import numpy as np\n",
    "import requests\n",
    "from io import BytesIO\n",
    "from math import trunc\n",
    "from PIL import Image as PILImage\n",
    "from PIL import ImageDraw as PILImageDraw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.486614Z",
     "start_time": "2019-07-27T08:58:05.443733Z"
    }
   },
   "outputs": [],
   "source": [
    "# Load the dataset json\n",
    "class CocoDataset():\n",
    "    def __init__(self, annotation_path, image_dir):\n",
    "        self.annotation_path = annotation_path\n",
    "        self.image_dir = image_dir\n",
    "        self.colors = ['blue', 'purple', 'red', 'green', 'orange', 'salmon', 'pink', 'gold',\n",
    "                       'orchid', 'slateblue', 'limegreen', 'seagreen', 'darkgreen', 'olive',\n",
    "                       'teal', 'aquamarine', 'steelblue', 'powderblue', 'dodgerblue', 'navy',\n",
    "                       'magenta', 'sienna', 'maroon']\n",
    "\n",
    "        json_file = open(self.annotation_path,encoding=\"utf-8\")\n",
    "        self.coco = json.load(json_file,encoding=\"utf-8\")\n",
    "        json_file.close()\n",
    "\n",
    "        self.process_info()\n",
    "        self.process_licenses()\n",
    "        self.process_categories()\n",
    "        self.process_images()\n",
    "        self.process_segmentations()\n",
    "\n",
    "    def display_info(self):\n",
    "        print('Dataset Info:')\n",
    "        print('=============')\n",
    "        if self.info is None:\n",
    "            return\n",
    "        for key, item in self.info.items():\n",
    "            print('  {}: {}'.format(key, item))\n",
    "\n",
    "        requirements = [['description', str],\n",
    "                        ['url', str],\n",
    "                        ['version', str],\n",
    "                        ['year', int],\n",
    "                        ['contributor', str],\n",
    "                        ['date_created', str]]\n",
    "        for req, req_type in requirements:\n",
    "            if req not in self.info:\n",
    "                print('ERROR: {} is missing'.format(req))\n",
    "            elif type(self.info[req]) != req_type:\n",
    "                print('ERROR: {} should be type {}'.format(req, str(req_type)))\n",
    "        print('')\n",
    "\n",
    "    def display_licenses(self):\n",
    "        print('Licenses:')\n",
    "        print('=========')\n",
    "\n",
    "        if self.licenses is None:\n",
    "            return\n",
    "        requirements = [['id', int],\n",
    "                        ['url', str],\n",
    "                        ['name', str]]\n",
    "        for license in self.licenses:\n",
    "            for key, item in license.items():\n",
    "                print('  {}: {}'.format(key, item))\n",
    "            for req, req_type in requirements:\n",
    "                if req not in license:\n",
    "                    print('ERROR: {} is missing'.format(req))\n",
    "                elif type(license[req]) != req_type:\n",
    "                    print('ERROR: {} should be type {}'.format(\n",
    "                        req, str(req_type)))\n",
    "            print('')\n",
    "        print('')\n",
    "\n",
    "    def display_categories(self):\n",
    "        print('Categories:')\n",
    "        print('=========')\n",
    "        for sc_key, sc_val in self.super_categories.items():\n",
    "            print('  super_category: {}'.format(sc_key))\n",
    "            for cat_id in sc_val:\n",
    "                print('    id {}: {}'.format(\n",
    "                    cat_id, self.categories[cat_id]['name']))\n",
    "            print('')\n",
    "\n",
    "    def display_image(self, image_id, show_polys=True, show_bbox=True, show_crowds=True, use_url=False):\n",
    "        print('Image:')\n",
    "        print('======')\n",
    "        if image_id == 'random':\n",
    "            image_id = random.choice(list(self.images.keys()))\n",
    "\n",
    "        # Print the image info\n",
    "        image = self.images[image_id]\n",
    "        for key, val in image.items():\n",
    "            print('  {}: {}'.format(key, val))\n",
    "\n",
    "        # Open the image\n",
    "        if use_url:\n",
    "            image_path = image['coco_url']\n",
    "            response = requests.get(image_path)\n",
    "            image = PILImage.open(BytesIO(response.content))\n",
    "\n",
    "        else:\n",
    "            # image_path = os.path.join(self.image_dir, image['file_name'])\n",
    "            image_path = \"{}/{}\".format(self.image_dir, image['file_name'])\n",
    "            image = PILImage.open(image_path)\n",
    "\n",
    "        # Calculate the size and adjusted display size\n",
    "        max_width = 600\n",
    "        image_width, image_height = image.size\n",
    "        adjusted_width = min(image_width, max_width)\n",
    "        adjusted_ratio = adjusted_width / image_width\n",
    "        adjusted_height = adjusted_ratio * image_height\n",
    "\n",
    "        # Create list of polygons to be drawn\n",
    "        polygons = {}\n",
    "        bbox_polygons = {}\n",
    "        rle_regions = {}\n",
    "        poly_colors = {}\n",
    "        bbox_categories = {}\n",
    "        print('  segmentations ({}):'.format(\n",
    "            len(self.segmentations[image_id])))\n",
    "        for i, segm in enumerate(self.segmentations[image_id]):\n",
    "            polygons_list = []\n",
    "            if segm['iscrowd'] != 0:\n",
    "                # Gotta decode the RLE\n",
    "                px = 0\n",
    "                x, y = 0, 0\n",
    "                rle_list = []\n",
    "                for j, counts in enumerate(segm['segmentation']['counts']):\n",
    "                    if j % 2 == 0:\n",
    "                        # Empty pixels\n",
    "                        px += counts\n",
    "                    else:\n",
    "                        # Need to draw on these pixels, since we are drawing in vector form,\n",
    "                        # we need to draw horizontal lines on the image\n",
    "                        x_start = trunc(\n",
    "                            trunc(px / image_height) * adjusted_ratio)\n",
    "                        y_start = trunc(px % image_height * adjusted_ratio)\n",
    "                        px += counts\n",
    "                        x_end = trunc(trunc(px / image_height)\n",
    "                                      * adjusted_ratio)\n",
    "                        y_end = trunc(px % image_height * adjusted_ratio)\n",
    "                        if x_end == x_start:\n",
    "                            # This is only on one line\n",
    "                            rle_list.append(\n",
    "                                {'x': x_start, 'y': y_start, 'width': 1, 'height': (y_end - y_start)})\n",
    "                        if x_end > x_start:\n",
    "                            # This spans more than one line\n",
    "                            # Insert top line first\n",
    "                            rle_list.append(\n",
    "                                {'x': x_start, 'y': y_start, 'width': 1, 'height': (image_height - y_start)})\n",
    "\n",
    "                            # Insert middle lines if needed\n",
    "                            lines_spanned = x_end - x_start + 1  # total number of lines spanned\n",
    "                            full_lines_to_insert = lines_spanned - 2\n",
    "                            if full_lines_to_insert > 0:\n",
    "                                full_lines_to_insert = trunc(\n",
    "                                    full_lines_to_insert * adjusted_ratio)\n",
    "                                rle_list.append(\n",
    "                                    {'x': (x_start + 1), 'y': 0, 'width': full_lines_to_insert, 'height': image_height})\n",
    "\n",
    "                            # Insert bottom line\n",
    "                            rle_list.append(\n",
    "                                {'x': x_end, 'y': 0, 'width': 1, 'height': y_end})\n",
    "                if len(rle_list) > 0:\n",
    "                    rle_regions[segm['id']] = rle_list\n",
    "            else:\n",
    "                # Add the polygon segmentation\n",
    "                for segmentation_points in segm['segmentation']:\n",
    "                    segmentation_points = np.multiply(\n",
    "                        segmentation_points, adjusted_ratio).astype(int)\n",
    "                    polygons_list.append(\n",
    "                        str(segmentation_points).lstrip('[').rstrip(']'))\n",
    "            polygons[segm['id']] = polygons_list\n",
    "            if i < len(self.colors):\n",
    "                poly_colors[segm['id']] = self.colors[i]\n",
    "            else:\n",
    "                poly_colors[segm['id']] = 'white'\n",
    "\n",
    "            bbox = segm['bbox']\n",
    "            bbox_points = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1],\n",
    "                           bbox[0] + bbox[2], bbox[1] +\n",
    "                           bbox[3], bbox[0], bbox[1] + bbox[3],\n",
    "                           bbox[0], bbox[1]]\n",
    "            bbox_points = np.multiply(bbox_points, adjusted_ratio).astype(int)\n",
    "            bbox_polygons[segm['id']] = str(\n",
    "                bbox_points).lstrip('[').rstrip(']')\n",
    "            bbox_categories[segm['id']] = self.categories[segm['category_id']]\n",
    "            # Print details\n",
    "            print('    {}:{}:{}'.format(\n",
    "                segm['id'], poly_colors[segm['id']], self.categories[segm['category_id']]))\n",
    "\n",
    "        # Draw segmentation polygons on image\n",
    "        html = '<div class=\"container\" style=\"position:relative;\">'\n",
    "        html += '<img src=\"{}\" style=\"position:relative;top:0px;left:0px;width:{}px;\">'.format(\n",
    "            image_path, adjusted_width)\n",
    "        html += '<div class=\"svgclass\"><svg width=\"{}\" height=\"{}\">'.format(\n",
    "            adjusted_width, adjusted_height)\n",
    "\n",
    "        if show_polys:\n",
    "            for seg_id, points_list in polygons.items():\n",
    "                fill_color = poly_colors[seg_id]\n",
    "                stroke_color = poly_colors[seg_id]\n",
    "                for points in points_list:\n",
    "                    html += '<polygon points=\"{}\" style=\"fill:{}; stroke:{}; stroke-width:1; fill-opacity:0.5\" />'.format(\n",
    "                        points, fill_color, stroke_color)\n",
    "\n",
    "        if show_crowds:\n",
    "            for seg_id, rect_list in rle_regions.items():\n",
    "                fill_color = poly_colors[seg_id]\n",
    "                stroke_color = poly_colors[seg_id]\n",
    "                for rect_def in rect_list:\n",
    "                    x, y = rect_def['x'], rect_def['y']\n",
    "                    w, h = rect_def['width'], rect_def['height']\n",
    "                    html += '<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" style=\"fill:{}; stroke:{}; stroke-width:1; fill-opacity:0.5; stroke-opacity:0.5\" />'.format(\n",
    "                        x, y, w, h, fill_color, stroke_color)\n",
    "\n",
    "        if show_bbox:\n",
    "            for seg_id, points in bbox_polygons.items():\n",
    "                x, y = [int(i) for i in points.split()[:2]]\n",
    "                html += '<text x=\"{}\" y=\"{}\" fill=\"yellow\">{}</text>'.format(\n",
    "                    x, y, bbox_categories[seg_id][\"name\"])\n",
    "                fill_color = poly_colors[seg_id]\n",
    "                stroke_color = poly_colors[seg_id]\n",
    "                html += '<polygon points=\"{}\" style=\"fill:{}; stroke:{}; stroke-width:1; fill-opacity:0\" />'.format(\n",
    "                    points, fill_color, stroke_color)\n",
    "\n",
    "        html += '</svg></div>'\n",
    "        html += '</div>'\n",
    "        html += '<style>'\n",
    "        html += '.svgclass { position:absolute; top:0px; left:0px;}'\n",
    "        html += '</style>'\n",
    "        return html\n",
    "\n",
    "    def process_info(self):\n",
    "        self.info = self.coco.get('info')\n",
    "\n",
    "    def process_licenses(self):\n",
    "        self.licenses = self.coco.get('licenses')\n",
    "\n",
    "    def process_categories(self):\n",
    "        self.categories = {}\n",
    "        self.super_categories = {}\n",
    "        for category in self.coco['categories']:\n",
    "            cat_id = category['id']\n",
    "            super_category = category['supercategory']\n",
    "\n",
    "            # Add category to the categories dict\n",
    "            if cat_id not in self.categories:\n",
    "                self.categories[cat_id] = category\n",
    "            else:\n",
    "                print(\"ERROR: Skipping duplicate category id: {}\".format(category))\n",
    "\n",
    "            # Add category to super_categories dict\n",
    "            if super_category not in self.super_categories:\n",
    "                # Create a new set with the category id\n",
    "                self.super_categories[super_category] = {cat_id}\n",
    "            else:\n",
    "                self.super_categories[super_category] |= {\n",
    "                    cat_id}  # Add category id to the set\n",
    "\n",
    "    def process_images(self):\n",
    "        self.images = {}\n",
    "        for image in self.coco['images']:\n",
    "            image_id = image['id']\n",
    "            if image_id in self.images:\n",
    "                print(\"ERROR: Skipping duplicate image id: {}\".format(image))\n",
    "            else:\n",
    "                self.images[image_id] = image\n",
    "\n",
    "    def process_segmentations(self):\n",
    "        self.segmentations = {}\n",
    "        for segmentation in self.coco['annotations']:\n",
    "            image_id = segmentation['image_id']\n",
    "            if image_id not in self.segmentations:\n",
    "                self.segmentations[image_id] = []\n",
    "            self.segmentations[image_id].append(segmentation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.510585Z",
     "start_time": "2019-07-27T08:58:05.489608Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset Info:\n",
      "=============\n",
      "Licenses:\n",
      "=========\n",
      "Categories:\n",
      "=========\n",
      "  super_category: pig\n",
      "    id 0: pig\n",
      "\n"
     ]
    }
   ],
   "source": [
    "annotation_path = \"trainval.json\"\n",
    "image_dir = \"pigimage\"\n",
    "\n",
    "coco_dataset = CocoDataset(annotation_path, image_dir)\n",
    "coco_dataset.display_info()\n",
    "coco_dataset.display_licenses()\n",
    "coco_dataset.display_categories()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.520552Z",
     "start_time": "2019-07-27T08:58:05.512547Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 {'height': 720, 'width': 1280, 'id': 0, 'file_name': '0603pig10017.jpg'}\n",
      "1 {'height': 720, 'width': 1280, 'id': 1, 'file_name': '0603pig10027.jpg'}\n",
      "2 {'height': 720, 'width': 1280, 'id': 2, 'file_name': '0603pig10146.jpg'}\n",
      "3 {'height': 720, 'width': 1280, 'id': 3, 'file_name': '0603pig10244.jpg'}\n",
      "4 {'height': 720, 'width': 1280, 'id': 4, 'file_name': '0603pig10289.jpg'}\n",
      "5 {'height': 720, 'width': 1280, 'id': 5, 'file_name': '0603pig10459.jpg'}\n",
      "6 {'height': 720, 'width': 1280, 'id': 6, 'file_name': '0603pig10537.jpg'}\n",
      "7 {'height': 720, 'width': 1280, 'id': 7, 'file_name': '0603pig10570.jpg'}\n",
      "8 {'height': 720, 'width': 1280, 'id': 8, 'file_name': '0603pig10624.jpg'}\n",
      "9 {'height': 720, 'width': 1280, 'id': 9, 'file_name': '0603pig10736.jpg'}\n",
      "10 {'height': 720, 'width': 1280, 'id': 10, 'file_name': '0603pig10739.jpg'}\n",
      "11 {'height': 720, 'width': 1280, 'id': 11, 'file_name': '0603pig11128.jpg'}\n",
      "12 {'height': 720, 'width': 1280, 'id': 12, 'file_name': '0603pig11367.jpg'}\n",
      "13 {'height': 720, 'width': 1280, 'id': 13, 'file_name': '0603pig11371.jpg'}\n",
      "14 {'height': 720, 'width': 1280, 'id': 14, 'file_name': '0603pig11436.jpg'}\n",
      "15 {'height': 720, 'width': 1280, 'id': 15, 'file_name': '0603pig11437.jpg'}\n",
      "16 {'height': 720, 'width': 1280, 'id': 16, 'file_name': '0603pig11439.jpg'}\n",
      "17 {'height': 720, 'width': 1280, 'id': 17, 'file_name': '0603pig11441.jpg'}\n",
      "18 {'height': 720, 'width': 1280, 'id': 18, 'file_name': '0603pig11442.jpg'}\n",
      "19 {'height': 720, 'width': 1280, 'id': 19, 'file_name': '0603pig11443.jpg'}\n",
      "20 {'height': 720, 'width': 1280, 'id': 20, 'file_name': '0603pig11444.jpg'}\n",
      "21 {'height': 720, 'width': 1280, 'id': 21, 'file_name': '0603pig11453.jpg'}\n",
      "22 {'height': 720, 'width': 1280, 'id': 22, 'file_name': '0603pig11454.jpg'}\n",
      "23 {'height': 720, 'width': 1280, 'id': 23, 'file_name': '0603pig11455.jpg'}\n",
      "24 {'height': 720, 'width': 1280, 'id': 24, 'file_name': '0603pig11456.jpg'}\n",
      "25 {'height': 720, 'width': 1280, 'id': 25, 'file_name': '0603pig11457.jpg'}\n",
      "26 {'height': 720, 'width': 1280, 'id': 26, 'file_name': '0603pig11468.jpg'}\n",
      "27 {'height': 720, 'width': 1280, 'id': 27, 'file_name': '0603pig11469.jpg'}\n",
      "28 {'height': 720, 'width': 1280, 'id': 28, 'file_name': '0603pig11471.jpg'}\n",
      "29 {'height': 720, 'width': 1280, 'id': 29, 'file_name': '0603pig11472.jpg'}\n",
      "30 {'height': 720, 'width': 1280, 'id': 30, 'file_name': '0603pig11473.jpg'}\n",
      "31 {'height': 720, 'width': 1280, 'id': 31, 'file_name': '0603pig11474.jpg'}\n",
      "32 {'height': 720, 'width': 1280, 'id': 32, 'file_name': '0603pig11591.jpg'}\n",
      "33 {'height': 720, 'width': 1280, 'id': 33, 'file_name': '0603pig11592.jpg'}\n",
      "34 {'height': 720, 'width': 1280, 'id': 34, 'file_name': '0603pig11622.jpg'}\n",
      "35 {'height': 720, 'width': 1280, 'id': 35, 'file_name': '0603pig11684.jpg'}\n",
      "36 {'height': 720, 'width': 1280, 'id': 36, 'file_name': '0603pig11685.jpg'}\n",
      "37 {'height': 720, 'width': 1280, 'id': 37, 'file_name': '0603pig11687.jpg'}\n",
      "38 {'height': 720, 'width': 1280, 'id': 38, 'file_name': '0603pig11689.jpg'}\n",
      "39 {'height': 720, 'width': 1280, 'id': 39, 'file_name': '0603pig11700.jpg'}\n",
      "40 {'height': 720, 'width': 1280, 'id': 40, 'file_name': '0603pig11701.jpg'}\n",
      "41 {'height': 720, 'width': 1280, 'id': 41, 'file_name': '0603pig11702.jpg'}\n",
      "42 {'height': 720, 'width': 1280, 'id': 42, 'file_name': '0603pig11703.jpg'}\n",
      "43 {'height': 720, 'width': 1280, 'id': 43, 'file_name': '0603pig11705.jpg'}\n",
      "44 {'height': 720, 'width': 1280, 'id': 44, 'file_name': '0603pig11707.jpg'}\n",
      "45 {'height': 720, 'width': 1280, 'id': 45, 'file_name': '0603pig11774.jpg'}\n",
      "46 {'height': 720, 'width': 1280, 'id': 46, 'file_name': '060810018.jpg'}\n",
      "47 {'height': 720, 'width': 1280, 'id': 47, 'file_name': '060810019.jpg'}\n",
      "48 {'height': 720, 'width': 1280, 'id': 48, 'file_name': '060810194.jpg'}\n",
      "49 {'height': 720, 'width': 1280, 'id': 49, 'file_name': '060810211.jpg'}\n",
      "50 {'height': 720, 'width': 1280, 'id': 50, 'file_name': '060810406.jpg'}\n",
      "51 {'height': 720, 'width': 1280, 'id': 51, 'file_name': '060810729.jpg'}\n",
      "52 {'height': 720, 'width': 1280, 'id': 52, 'file_name': '060810736.jpg'}\n",
      "53 {'height': 720, 'width': 1280, 'id': 53, 'file_name': '060810794.jpg'}\n",
      "54 {'height': 720, 'width': 1280, 'id': 54, 'file_name': '060811024.jpg'}\n",
      "55 {'height': 720, 'width': 1280, 'id': 55, 'file_name': '060811027.jpg'}\n",
      "56 {'height': 720, 'width': 1280, 'id': 56, 'file_name': '060811267.jpg'}\n",
      "57 {'height': 720, 'width': 1280, 'id': 57, 'file_name': '060811474.jpg'}\n",
      "58 {'height': 720, 'width': 1280, 'id': 58, 'file_name': '060811550.jpg'}\n",
      "59 {'height': 720, 'width': 1280, 'id': 59, 'file_name': '060811676.jpg'}\n",
      "60 {'height': 720, 'width': 1280, 'id': 60, 'file_name': '060811715.jpg'}\n",
      "61 {'height': 720, 'width': 1280, 'id': 61, 'file_name': '060811778.jpg'}\n",
      "62 {'height': 720, 'width': 1280, 'id': 62, 'file_name': '0608w10009.jpg'}\n",
      "63 {'height': 720, 'width': 1280, 'id': 63, 'file_name': '0608w10043.jpg'}\n",
      "64 {'height': 720, 'width': 1280, 'id': 64, 'file_name': '0608w10146.jpg'}\n",
      "65 {'height': 720, 'width': 1280, 'id': 65, 'file_name': '0608w10168.jpg'}\n",
      "66 {'height': 720, 'width': 1280, 'id': 66, 'file_name': '0608w10378.jpg'}\n",
      "67 {'height': 720, 'width': 1280, 'id': 67, 'file_name': '0608w10566.jpg'}\n",
      "68 {'height': 1080, 'width': 1920, 'id': 68, 'file_name': '10.jpg'}\n",
      "69 {'height': 1080, 'width': 1920, 'id': 69, 'file_name': '100.jpg'}\n",
      "70 {'height': 1080, 'width': 1920, 'id': 70, 'file_name': '110.jpg'}\n",
      "71 {'height': 1080, 'width': 1920, 'id': 71, 'file_name': '120.jpg'}\n",
      "72 {'height': 1080, 'width': 1920, 'id': 72, 'file_name': '130.jpg'}\n",
      "73 {'height': 1080, 'width': 1920, 'id': 73, 'file_name': '140.jpg'}\n",
      "74 {'height': 1080, 'width': 1920, 'id': 74, 'file_name': '150.jpg'}\n",
      "75 {'height': 1080, 'width': 1920, 'id': 75, 'file_name': '160.jpg'}\n",
      "76 {'height': 1080, 'width': 1920, 'id': 76, 'file_name': '170.jpg'}\n",
      "77 {'height': 1080, 'width': 1920, 'id': 77, 'file_name': '180.jpg'}\n",
      "78 {'height': 1080, 'width': 1920, 'id': 78, 'file_name': '190.jpg'}\n",
      "79 {'height': 1080, 'width': 1920, 'id': 79, 'file_name': '20.jpg'}\n",
      "80 {'height': 1080, 'width': 1920, 'id': 80, 'file_name': '200.jpg'}\n",
      "81 {'height': 1080, 'width': 1920, 'id': 81, 'file_name': '2080.jpg'}\n",
      "82 {'height': 1080, 'width': 1920, 'id': 82, 'file_name': '2090.jpg'}\n",
      "83 {'height': 1080, 'width': 1920, 'id': 83, 'file_name': '210.jpg'}\n",
      "84 {'height': 1080, 'width': 1920, 'id': 84, 'file_name': '2100.jpg'}\n",
      "85 {'height': 1080, 'width': 1920, 'id': 85, 'file_name': '2110.jpg'}\n",
      "86 {'height': 1080, 'width': 1920, 'id': 86, 'file_name': '2120.jpg'}\n",
      "87 {'height': 1080, 'width': 1920, 'id': 87, 'file_name': '2130.jpg'}\n",
      "88 {'height': 1080, 'width': 1920, 'id': 88, 'file_name': '2140.jpg'}\n",
      "89 {'height': 1080, 'width': 1920, 'id': 89, 'file_name': '2150.jpg'}\n",
      "90 {'height': 1080, 'width': 1920, 'id': 90, 'file_name': '2160.jpg'}\n",
      "91 {'height': 1080, 'width': 1920, 'id': 91, 'file_name': '2170.jpg'}\n",
      "92 {'height': 1080, 'width': 1920, 'id': 92, 'file_name': '2180.jpg'}\n",
      "93 {'height': 1080, 'width': 1920, 'id': 93, 'file_name': '2190.jpg'}\n",
      "94 {'height': 1080, 'width': 1920, 'id': 94, 'file_name': '220.jpg'}\n",
      "95 {'height': 1080, 'width': 1920, 'id': 95, 'file_name': '2200.jpg'}\n",
      "96 {'height': 1080, 'width': 1920, 'id': 96, 'file_name': '2240.jpg'}\n",
      "97 {'height': 1080, 'width': 1920, 'id': 97, 'file_name': '2280.jpg'}\n",
      "98 {'height': 1080, 'width': 1920, 'id': 98, 'file_name': '230.jpg'}\n",
      "99 {'height': 1080, 'width': 1920, 'id': 99, 'file_name': '2300.jpg'}\n",
      "100 {'height': 1080, 'width': 1920, 'id': 100, 'file_name': '2310.jpg'}\n",
      "101 {'height': 1080, 'width': 1920, 'id': 101, 'file_name': '2320.jpg'}\n",
      "102 {'height': 1080, 'width': 1920, 'id': 102, 'file_name': '240.jpg'}\n",
      "103 {'height': 1080, 'width': 1920, 'id': 103, 'file_name': '250.jpg'}\n",
      "104 {'height': 1080, 'width': 1920, 'id': 104, 'file_name': '260.jpg'}\n",
      "105 {'height': 1080, 'width': 1920, 'id': 105, 'file_name': '270.jpg'}\n",
      "106 {'height': 1080, 'width': 1920, 'id': 106, 'file_name': '290.jpg'}\n",
      "107 {'height': 1080, 'width': 1920, 'id': 107, 'file_name': '30.jpg'}\n",
      "108 {'height': 1080, 'width': 1920, 'id': 108, 'file_name': '310.jpg'}\n",
      "109 {'height': 1080, 'width': 1920, 'id': 109, 'file_name': '320.jpg'}\n",
      "110 {'height': 1080, 'width': 1920, 'id': 110, 'file_name': '330.jpg'}\n",
      "111 {'height': 1080, 'width': 1920, 'id': 111, 'file_name': '40.jpg'}\n",
      "112 {'height': 1080, 'width': 1920, 'id': 112, 'file_name': '50.jpg'}\n",
      "113 {'height': 1080, 'width': 1920, 'id': 113, 'file_name': '60.jpg'}\n",
      "114 {'height': 1080, 'width': 1920, 'id': 114, 'file_name': '70.jpg'}\n",
      "115 {'height': 1080, 'width': 1920, 'id': 115, 'file_name': '80.jpg'}\n",
      "116 {'height': 1080, 'width': 1920, 'id': 116, 'file_name': '90.jpg'}\n",
      "117 {'height': 1080, 'width': 1920, 'id': 117, 'file_name': '980.jpg'}\n",
      "118 {'height': 720, 'width': 1280, 'id': 118, 'file_name': 'pig18243.jpg'}\n",
      "119 {'height': 720, 'width': 1280, 'id': 119, 'file_name': 'pig18282.jpg'}\n",
      "120 {'height': 720, 'width': 1280, 'id': 120, 'file_name': 'pig18369.jpg'}\n",
      "121 {'height': 720, 'width': 1280, 'id': 121, 'file_name': 'pig18432.jpg'}\n",
      "122 {'height': 720, 'width': 1280, 'id': 122, 'file_name': 'pig18468.jpg'}\n",
      "123 {'height': 720, 'width': 1280, 'id': 123, 'file_name': 'pig18535.jpg'}\n",
      "124 {'height': 720, 'width': 1280, 'id': 124, 'file_name': 'pig18604.jpg'}\n",
      "125 {'height': 720, 'width': 1280, 'id': 125, 'file_name': 'pig18667.jpg'}\n",
      "126 {'height': 720, 'width': 1280, 'id': 126, 'file_name': 'pig18687.jpg'}\n",
      "127 {'height': 720, 'width': 1280, 'id': 127, 'file_name': 'pig18791.jpg'}\n",
      "128 {'height': 720, 'width': 1280, 'id': 128, 'file_name': 'pig18807.jpg'}\n",
      "129 {'height': 720, 'width': 1280, 'id': 129, 'file_name': 'pig18874.jpg'}\n",
      "130 {'height': 720, 'width': 1280, 'id': 130, 'file_name': 'pig18915.jpg'}\n",
      "131 {'height': 720, 'width': 1280, 'id': 131, 'file_name': 'pig19134.jpg'}\n",
      "132 {'height': 720, 'width': 1280, 'id': 132, 'file_name': 'pig19216.jpg'}\n",
      "133 {'height': 1080, 'width': 1920, 'id': 133, 'file_name': '赶猪通道-移动_1126538195.jpg'}\n",
      "134 {'height': 1080, 'width': 1920, 'id': 134, 'file_name': '赶猪通道-移动_112653860.jpg'}\n",
      "135 {'height': 1080, 'width': 1920, 'id': 135, 'file_name': '赶猪通道-移动_1158684120.jpg'}\n",
      "136 {'height': 1080, 'width': 1920, 'id': 136, 'file_name': '赶猪通道-移动_1158684585.jpg'}\n",
      "137 {'height': 1080, 'width': 1920, 'id': 137, 'file_name': '赶猪通道-移动_2600736105.jpg'}\n",
      "138 {'height': 1080, 'width': 1920, 'id': 138, 'file_name': '赶猪通道-移动_260073645.jpg'}\n",
      "139 {'height': 1080, 'width': 1920, 'id': 139, 'file_name': '赶猪通道-移动_265239230.jpg'}\n",
      "140 {'height': 1080, 'width': 1920, 'id': 140, 'file_name': '赶猪通道-移动_2729694105.jpg'}\n",
      "141 {'height': 1080, 'width': 1920, 'id': 141, 'file_name': '赶猪通道-移动_2729694285.jpg'}\n",
      "142 {'height': 1080, 'width': 1920, 'id': 142, 'file_name': '赶猪通道-移动_3431144225.jpg'}\n",
      "143 {'height': 1080, 'width': 1920, 'id': 143, 'file_name': '赶猪通道-移动_3431144330.jpg'}\n",
      "144 {'height': 1080, 'width': 1920, 'id': 144, 'file_name': '赶猪通道-移动_3431144360.jpg'}\n",
      "145 {'height': 1080, 'width': 1920, 'id': 145, 'file_name': '赶猪通道-移动_3431144375.jpg'}\n",
      "146 {'height': 1080, 'width': 1920, 'id': 146, 'file_name': '赶猪通道-移动_3431144450.jpg'}\n",
      "147 {'height': 1080, 'width': 1920, 'id': 147, 'file_name': '赶猪通道-移动_3431144480.jpg'}\n",
      "148 {'height': 1080, 'width': 1920, 'id': 148, 'file_name': '赶猪通道-移动_3431144495.jpg'}\n",
      "149 {'height': 1080, 'width': 1920, 'id': 149, 'file_name': '赶猪通道-移动_3481622375.jpg'}\n",
      "150 {'height': 1080, 'width': 1920, 'id': 150, 'file_name': '赶猪通道-移动_3564818105.jpg'}\n",
      "151 {'height': 1080, 'width': 1920, 'id': 151, 'file_name': '赶猪通道-移动_3564818150.jpg'}\n",
      "152 {'height': 1080, 'width': 1920, 'id': 152, 'file_name': '赶猪通道-移动_3564818225.jpg'}\n",
      "153 {'height': 1080, 'width': 1920, 'id': 153, 'file_name': '赶猪通道-移动_3564818345.jpg'}\n",
      "154 {'height': 1080, 'width': 1920, 'id': 154, 'file_name': '赶猪通道-移动_3619393165.jpg'}\n",
      "155 {'height': 1080, 'width': 1920, 'id': 155, 'file_name': '赶猪通道-移动_361939330.jpg'}\n",
      "156 {'height': 1080, 'width': 1920, 'id': 156, 'file_name': '赶猪通道-移动_4080971105.jpg'}\n",
      "157 {'height': 1080, 'width': 1920, 'id': 157, 'file_name': '赶猪通道-移动_4080971195.jpg'}\n",
      "158 {'height': 1080, 'width': 1920, 'id': 158, 'file_name': '赶猪通道-移动_4080971240.jpg'}\n",
      "159 {'height': 1080, 'width': 1920, 'id': 159, 'file_name': '赶猪通道-移动_408097190.jpg'}\n"
     ]
    }
   ],
   "source": [
    "for k, v in coco_dataset.images.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.572387Z",
     "start_time": "2019-07-27T08:58:05.523519Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image:\n",
      "======\n",
      "  height: 720\n",
      "  width: 1280\n",
      "  id: 1\n",
      "  file_name: 0603pig10027.jpg\n",
      "  segmentations (3):\n",
      "    4:blue:{'supercategory': 'pig', 'id': 0, 'name': 'pig'}\n",
      "    5:purple:{'supercategory': 'pig', 'id': 0, 'name': 'pig'}\n",
      "    6:red:{'supercategory': 'pig', 'id': 0, 'name': 'pig'}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"container\" style=\"position:relative;\"><img src=\"pigimage/0603pig10027.jpg\" style=\"position:relative;top:0px;left:0px;width:600px;\"><div class=\"svgclass\"><svg width=\"600\" height=\"337.5\"><polygon points=\"155 117 148 120 144 130 141 139 150 133 152 134 152 142 149 150 157 154\n",
       " 160 147 169 145 178 143 179 137 182 137 186 130 196 129 202 128 220 120\n",
       " 231 118 248 115 270 115 283 112 294 108 297  99 299  91 307  90 306  85\n",
       " 298  84 292  74 281  68 263  67 241  68 222  72 204  76 185  84 167  95\n",
       " 160 111\" style=\"fill:blue; stroke:blue; stroke-width:1; fill-opacity:0.5\" /><polygon points=\"215 125 214 122 215 120 212 117 208 113 201 112 196 115 196 120 200 125\n",
       " 202 128 201 129 197 130 192 130 185 131 182 133 180 137 182 142 186 143\n",
       " 191 144 188 148 186 150 189 153 193 154 200 156 204 155 211 155 212 152\n",
       " 219 152 225 154 229 156 238 157 248 157 261 156 273 155 289 152 298 151\n",
       " 311 149 326 147 336 144 346 139 353 128 353 117 354 109 351 105 347 105\n",
       " 340 102 333 100 318 102 314 105 304 108 291 112 277 114 265 116 250 117\n",
       " 240 118 232 120 224 122\" style=\"fill:purple; stroke:purple; stroke-width:1; fill-opacity:0.5\" /><polygon points=\"537 183 545 188 552 197 558 212 564 229 568 249 568 272 568 286 568 303\n",
       " 564 319 555 337 527 337 524 332 519 304 514 274 514 264 527 269 518 245\n",
       " 506 223 501 193 504 192 509 192 526 188\" style=\"fill:red; stroke:red; stroke-width:1; fill-opacity:0.5\" /><text x=\"141\" y=\"67\" fill=\"yellow\">pig</text><polygon points=\"141  67 307  67 307 154 141 154 141  67\" style=\"fill:blue; stroke:blue; stroke-width:1; fill-opacity:0\" /><text x=\"180\" y=\"99\" fill=\"yellow\">pig</text><polygon points=\"180  99 354  99 354 157 180 157 180  99\" style=\"fill:purple; stroke:purple; stroke-width:1; fill-opacity:0\" /><text x=\"501\" y=\"182\" fill=\"yellow\">pig</text><polygon points=\"501 182 568 182 568 337 501 337 501 182\" style=\"fill:red; stroke:red; stroke-width:1; fill-opacity:0\" /></svg></div></div><style>.svgclass { position:absolute; top:0px; left:0px;}</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html = coco_dataset.display_image(1, use_url=False)\n",
    "IPython.display.HTML(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-07-27T08:58:05.587373Z",
     "start_time": "2019-07-27T08:58:05.574383Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image:\n",
      "======\n",
      "  height: 720\n",
      "  width: 1280\n",
      "  id: 10\n",
      "  file_name: 0603pig10739.jpg\n",
      "  segmentations (1):\n",
      "    30:blue:{'supercategory': 'pig', 'id': 0, 'name': 'pig'}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"container\" style=\"position:relative;\"><img src=\"pigimage/0603pig10739.jpg\" style=\"position:relative;top:0px;left:0px;width:600px;\"><div class=\"svgclass\"><svg width=\"600\" height=\"337.5\"><polygon points=\"101 125  97 128  91 130  84 133  78 136  79 140  80 141  79 147  79 152\n",
       "  82 155  81 160  80 167  85 170  91 172  92 175 101 168 103 169 111 171\n",
       " 122 172 137 169 146 164 150 160 156 164 164 166 165 163 160 160 153 157\n",
       " 148 156 146 154 152 149 157 144 164 137 168 126 171 118 175 114 180 108\n",
       " 181 102 183  95 182  90 180  83 175  77 167  73 157  70 152  68 145  65\n",
       " 140  65 146  68 139  70 134  73 130  77 128  82 125  87 120  94 113 104\n",
       " 107 113 104 121\" style=\"fill:blue; stroke:blue; stroke-width:1; fill-opacity:0.5\" /><text x=\"78\" y=\"64\" fill=\"yellow\">pig</text><polygon points=\" 78  64 183  64 183 175  78 175  78  64\" style=\"fill:blue; stroke:blue; stroke-width:1; fill-opacity:0\" /></svg></div></div><style>.svgclass { position:absolute; top:0px; left:0px;}</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html = coco_dataset.display_image(10, use_url=False)\n",
    "IPython.display.HTML(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}